package cn.net.greenet.common.db_service;

import com.ambimmort.app.framework.uitls.Application;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author LiuShang 读取配置文件（属性文件）的工具类
 *
 */
public class ConfigManager {


    private static ConfigManager configManager;
    //properties.load(InputStream);读取属性文件
    private static Properties properties;

    private ConfigManager() {

        String configFile = "c3p0.properties";
//        String configFile="config/database.properties";
        properties = new Properties();
//		InputStream in=ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
//        properties = new Properties();

        File etcFile = new File(Application.getConfDir(),"configuration.properties");
        if(etcFile.exists()){
            configFile = etcFile.getAbsolutePath();
        }
        
        File devFile = new File("src/main/resources/config/configuration.properties");
        if(devFile.exists()){
            configFile = "src/main/resources/config/configuration.properties";
        }

        System.out.println("ConfigManager: "+configFile);
        try {
            InputStream in = new FileInputStream(configFile);
            properties.load(in);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static ConfigManager getInstance() {
        if (configManager == null) {
            configManager = new ConfigManager();
        }
        return configManager;
    }

    public String getString(String key) {
        return properties.getProperty(key);
    }

    public int getInt(String key) {
        return Integer.parseInt(properties.getProperty(key));
    }

    public static void main(String[] args) throws IOException {

        System.out.println(ConfigManager.getInstance().getString("c3p0.driverClass"));
    }
}
